<?php 
namespace Admin\Controller;
use Common\Controller\AdminBaseController;
class SingleController extends AdminBaseController{
	public function index(){
		$title = I('get.title/s','');
		$page = I('get.page/d',1);
		$new =  M('single');
		$sql = empty($title) ? '' : 'title like "%' . $title . '%"';
		$count = $new->where($sql)->count();
		$totalNum = 5;
		$totalPage = ceil($count/$totalNum);
		$firstC = ($page-1) * $totalNum;
		$list = $new->field('id,title,img,content,keyword,status')->where($sql)->order('id desc')->limit($firstC,$totalNum)->select();
		$this->assign('list',$list);
		$this->assign('title',$title);
		$this->assign('totalPage',$totalPage);
		$this->display();
	}
	public function changeStatus(){
		if(IS_POST){
			$id = I('post.id/d',0);
			if(is_null($id)){
				$this->ajaxReturn(['status'=>0,'msg'=>'操作有误']);
			}
			$content = M('single')->field('status')->where(['id'=>$id])->find();
			// var_dump($content);exit;
			if(empty($content['status'])){
				M('single')->where(['id'=>$id])->setField('status',1);
				$this->ajaxReturn(['status'=>1,'msg'=>'权限开启','data'=>1]);
			}else{
				M('single')->where(['id'=>$id])->setField('status',0);
				$this->ajaxReturn(['status'=>1,'msg'=>'权限禁止','data'=>0]);
			}
		}else{
			$this->ajaxReturn(['status'=>1,'msg'=>'操作有误']);
		}
	}
	public function del(){
		if(IS_POST){
			$id = I('post.id/d',0);
			if(empty($id)){
				$this->ajaxReturn(['status'=>0,'msg'=>'操作有误']);
			}
			$rulenum = M('Single')->where(['pid'=>$id])->count();
			if($rulenum > 0){
				$this->ajaxReturn(['status'=>0,'msg'=>'请先删除子栏目']);
			}
			$del = M('Single')->where(['id'=>$id])->delete();

			if($del){
				$this->ajaxReturn(['status'=>1,'msg'=>'删除成功']);
			}else{
				$this->ajaxReturn(['status'=>0,'msg'=>'删除失败']);
			}
		}else{
			$this->ajaxReturn(['status'=>0,'msg'=>'操作有误']);
		}
	}
	public function save(){
		if(IS_AJAX || IS_POST){
			$id = I('post.id/d',0);
			$title=I('post.title/s','');
			if(!$title){
				$this->ajaxReturn(['status'=>0,'msg'=>'标题不能为空']);
			}
			$pid=I('post.pid/s',0);
			$content=I('post.content/s','');
			if(!$content){
				$this->ajaxReturn(['status'=>0,'msg'=>'内容不能为空']);
			}
			$keyword=I('post.keyword/s','');
			if(!$keyword){
				$this->ajaxReturn(['status'=>0,'msg'=>'关键词不能为空']);
			}
			$status=I('post.status/d');
			$filePath = '';
			$new = M('single');
			$img = $new->field('img')->where(['id'=>$id])->find();
			if(($_FILES['img']['size']) > 0){
				$filePath = uploadFile($_FILES['img'],$imgPath='Upload/single/');
			}else{
				$filePath = $img['img'];
			}
			if(is_array($filePath)){
				$this->ajaxReturn($filePath);
			}
			
			if(empty($id)){
				$titles = $new->where(['title'=>$title])->find();
				if($titles){
					$this->ajaxReturn(['status'=>0,'msg'=>'标题已存在']);
				}
			}
			
			$data=['id'=>$id,'title'=>$title,'pid'=>$pid,'img'=>$filePath,'content'=>$content,'keyword'=>$keyword,'status'=>$status];
			$add = $new->add($data,[],true);
			if(!$add){
				$this->ajaxReturn(['status'=>0,'msg'=>'失败']);
			}
			$this->ajaxReturn(['status'=>1,'msg'=>'成功']);
		}	
	}
	public function edit(){
		if(IS_GET){
			$id=I('get.id/d',0);
			if(!$id){
				$this->error('你的操作有误!!!');
			}
			$listinfo = M('single')->field('id,title,content,img,status,keyword')->where(['id'=>$id])->find();
			$this->assign('listinfo',$listinfo);
			$this->display('single/add');
			
		}
	}
}







?>